<template>
  <div class="box">
    <div class="login_page">
      <h1>
        <img
          src="https://m.assets.shop.hisense.com/assets/files/hisense.46ef582304ee74ce.png"
          alt=""
        />后台管理系统
      </h1>
      <el-form ref="form" label-width="20%" :model="form" :rules="rules">
        <el-form-item label="用户名" prop="username" class="inp">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" class="inp" prop="password">
          <el-input v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="plain" @click="submitForm" class="btn1"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import authRouter from "../router/authRouter";
console.log("authRouter", authRouter);
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ required: true, message: "用户名不能为空" }],
        password: [{ required: true, message: "密码不能为空" }],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(async (valid) => {
        // console.log(this.form);
        if (valid) {
          //校验通过发起请求
          // this.$request
          //     .post("/users/login", this.form)
          //     .then(({ data }) => {
          //         console.log("data", data);
          //         if (data.code === 200) {
          //             //动态添加路由
          //             // this.$router.addRoute(authRouter)
          //             console.log(this.form);
          //             this.$router.push("/manage");
          //         } else {

          //             this.$message({
          //                 message: "账号或密码错误！",
          //                 center: true,
          //                 type: "error",
          //             });
          //         }
          //     });
          const data = await this.$store.dispatch("user/login", {
            ...this.form,
          });
          console.log(data);
          if (data.code === 200) {
            this.$router.push("/manage");
          }
        } else {
          // console.log("error submit!!");
          this.$message({
            message: "账号或密码不能为空！",
            center: true,
            type: "error",
          });
          return false;
        }
      });
    },
  },
};
</script>
<style scoped lang="scss" scope>
.box {
  width: 100%;
  height: 100vw;
  background-color: rgb(99, 99, 99);
  z-index: 999;
}
// .login_page{
//     color: rgb(94, 94, 94);

// }
.login_page {
  text-align: center;
  border-radius: 15px;
  background-color: #ffffff;
  width: 50%;
  position: absolute;
  transform: translateX(-50%) translateY(-50%);
  top: 30%;
  left: 50%;
  font-weight: bold;
}

h1 {
  margin-top: 50px;
  margin-bottom: 50px;
  // margin-top: 0;
  font-size: 28px;
  line-height: 28px;
  padding: 5px;
  text-align: center;
  img {
    width: 125px;
    height: 20px;
    margin-right: 15px;
  }
}

.inp {
  width: 90%;
  text-align: center;
}
.btn {
  margin-left: 30%;
}
.btn1 {
  background-color: orange;
  color: #fff;
}
</style>
